package com.github.thinwonton.activemq.showcase.cluster.mq;

import org.apache.activemq.command.ActiveMQTextMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.listener.SessionAwareMessageListener;

import javax.jms.Message;
import javax.jms.Session;


public class ConsumerSessionAwareMessageListener implements SessionAwareMessageListener<Message> {

    private static final Logger logger = LoggerFactory.getLogger(ConsumerSessionAwareMessageListener.class);

    /**
     * 消费者名
     */
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public synchronized void onMessage(Message message, Session session) {
        try {
            ActiveMQTextMessage msg = (ActiveMQTextMessage) message;
            final String ms = msg.getText();
            logger.info(" ==> [" + name + "] ---- receive message:  " + ms);
        } catch (Exception e) {
            logger.error("==>", e);
        }
    }
}
